package design.patterns.proxyMode.vectorproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2020/6/10 19:57
 */

public class Test {
    public static void main(String[] args) {
        List list  = null;
        list = (List) MyHandler.factory(new ArrayList<>(10));
        list.add("fef");
        list.get(0);
    }
}

class MyHandler implements InvocationHandler {
    private Object obj;

    public MyHandler(Object obj) {
        this.obj = obj;
    }

    public static Object factory(Object o){
        return Proxy.newProxyInstance(o.getClass().getClassLoader(),o.getClass().getInterfaces(),new MyHandler(o));
    }
    /**
     *
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before");
        Object o = method.invoke(obj, args);
        System.out.println("after");
        return o;
    }
}